<?php
declare (strict_types=1);

namespace app\common\queue\notify;

use app\admin\logic\market\ticket\Ticket;
use app\admin\logic\market\ticket\TicketCoupon;
use app\admin\logic\system\notify\NotifyNode;
use app\common\library\Queue;
use app\user\model\user\Account;
use think\facade\Request;

class EventTicketNotiy
{
    protected $templateCode = '';
    /**
     * 活动
     *
     * @var mixed
     */
    protected $ticketId;
    /**
     * 活动券
     *
     * @var mixed
     */
    protected $couponId;

    /**
     * 分页数
     *
     * @var int|mixed
     */
    protected $page;
    /**
     * 通知变量内容
     *
     * @var string
     */
    protected $content;

    public function run()
    {
        $data = Request::post();
        // 活动ID
        $this->ticketId = $ticketId = intval($data['ticket_id']);
        // 活动券
        $this->couponId = $couponId = intval($data['coupon_id']);
        // 分页数
        $this->page = $data['page'] ?? 1;
        // 查询活动信息
        $ticket = app(Ticket::class)->getInfoById($ticketId);
        // 活动是否启用
        if ($ticket->status != 1) {
            return;
        }
        // 查询活动券信息
        $coupon        = app(TicketCoupon::class)->getInfoById($couponId);
        $this->content = $coupon->coupon_type == 1 ? $coupon->price . '元' : $coupon->discount . '折';
        // 活动对象为沉默用户和全部用户
        switch ($ticket->target_user_type) {
            case 0:
                // 全部用户
                $this->templateCode = 'market_ticket_all';
                $this->matchUser();
                break;
            case 3:
                // 沉默用户: 截止 start_time 已有 day 天未登录
                $this->templateCode = 'market_ticket_silent';
                $extData            = $coupon->ext_data;
                $endTime            = $extData['start_time'] ?? time();
                $day                = $extData['day'] ?? 0;
                if ($day > 0) {
                    $this->matchUser([
                        ['last_login_time', '<', $endTime - $day * 86400],
                    ]);
                }
                break;
        }

    }

    /**
     * 匹配用户发送通知
     *
     * @param array $where
     * @param int   $page
     * @throws \think\db\exception\DbException
     */
    private function matchUser(array $where = [])
    {
        $datas     = app(Account::class)->where($where)->field(['phone'])->paginate(100);
        $notifyArr = [];
        /** @var Account $item */
        foreach ($datas->items() as $item) {
            // 批量推送
            $notifyArr[] = $item->getData('phone');
        }

        // 推送到发送短信
        Queue::push('EventTicketSendSms', [
            'phones' => $notifyArr,
            'code'   => $this->getSmsTemplateCode(),
            'var'    => [
                'price' => $this->content
            ]
        ]);

        // 继续推送任务
        if ($datas->hasPages()) {
            Queue::push('EventTicketNotify', [
                'ticket_id' => $this->ticketId,
                'coupon_id' => $this->couponId,
                'page'      => $this->page + 1
            ]);
        }
    }

    /**
     * 获取短信配置模板
     *
     * @return mixed
     */
    private function getSmsTemplateCode()
    {
        $nodeData = app(NotifyNode::class)->getNotifyNodeDataByKey($this->templateCode);

        return $nodeData->sms_template_code;
    }
}